Skip to content

feat: add support for phpstan>=2.1.12#47

Merged
nunomaduro merged 8 commits intopestphp:3.xfrom
maxwellmezadre:3.x
Apr 25, 2025
Merged

feat: add support for phpstan>=2.1.12#47
nunomaduro merged 8 commits intopestphp:3.xfrom
maxwellmezadre:3.x

Conversation

@maxwellmezadre
Copy link
Copy Markdown
Contributor

Since PHPStan 2.1.12 introduces an additional constructor parameter in NodeScopeResolver, this PR adjusts the constructor invocation for PHPStan ≥ 2.1.12.

CleanShot 2025-04-16 at 17 14 55

@nimah79
Copy link
Copy Markdown
Contributor

nimah79 commented Apr 17, 2025

But it needs another argument after the other arguments (narrowMethodScopeFromConstructor).

I think it'll be much better if the NodeScopeResolver is instantiated using named arguments. No need to check PHPStan version:

$nodeScopeResolver = new NodeScopeResolver( // @phpstan-ignore-line
    reflectionProvider: $reflectionProvider,
    initializerExprTypeResolver: $container->getByType(InitializerExprTypeResolver::class),
    reflector: $container->getService('betterReflectionReflector'), // @phpstan-ignore-line
    classReflectionExtensionRegistryProvider: $container->getByType(ClassReflectionExtensionRegistryProvider::class),
    parameterOutTypeExtensionProvider: $container->getByType(ParameterOutTypeExtensionProvider::class),
    parser: $container->getService('defaultAnalysisParser'), // @phpstan-ignore-line
    fileTypeMapper: $container->getByType(FileTypeMapper::class),
    stubPhpDocProvider: $container->getByType(StubPhpDocProvider::class),
    phpVersion: $container->getByType(PhpVersion::class),
    signatureMapProvider: $container->getByType(SignatureMapProvider::class),
    deprecationProvider: $container->getByType(DeprecationProvider::class),
    attributeReflectionFactory: $container->getByType(AttributeReflectionFactory::class),
    phpDocInheritanceResolver: $container->getByType(PhpDocInheritanceResolver::class),
    fileHelper: $container->getByType(FileHelper::class),
    typeSpecifier: $typeSpecifier, // @phpstan-ignore-line
    dynamicThrowTypeExtensionProvider: $container->getByType(DynamicThrowTypeExtensionProvider::class),
    readWritePropertiesExtensionProvider: $container->getByType(ReadWritePropertiesExtensionProvider::class),
    parameterClosureTypeExtensionProvider: $container->getByType(ParameterClosureTypeExtensionProvider::class),
    scopeFactory: $scopeFactory,
    polluteScopeWithLoopInitialAssignments: false,
    polluteScopeWithAlwaysIterableForeach: true,
    polluteScopeWithBlock: true, // @phpstan-ignore-line
    earlyTerminatingMethodCalls: [],
    earlyTerminatingFunctionCalls: [],
    universalObjectCratesClasses: [], // @phpstan-ignore-line
    implicitThrows: true,
    treatPhpDocTypesAsCertain: true,
    narrowMethodScopeFromConstructor: true,
);

@maxwellmezadre

@maxwellmezadre
Copy link
Copy Markdown
Contributor Author

I’ve switched the NodeScopeResolver instantiation to named arguments and added the extra narrowMethodScopeFromConstructor parameter as requested.
However, the PHPStan version check has to stay—other contributors and CI jobs may still be running different PHPStan releases, so removing the guard would break their builds. Keeping the check lets the code work reliably across the full range of supported versions.

@nimah79

@ondrejmirtes
Copy link
Copy Markdown

NodeScopeResolver constructor isn't covered by BC promise (https://phpstan.org/developing-extensions/backward-compatibility-promise) because it often changes.

The proper solution is to fetch NodeScopeResolver via Container::getByType().

@pelmered
Copy link
Copy Markdown

NodeScopeResolver constructor isn't covered by BC promise (https://phpstan.org/developing-extensions/backward-compatibility-promise) because it often changes.

The proper solution is to fetch NodeScopeResolver via Container::getByType().

Do you mean that all this code including all the version checks could be replaced with:
$nodeScopeResolver = Container::getByType(NodeScopeResolver::class)

@maxwellmezadre
Copy link
Copy Markdown
Contributor Author

maxwellmezadre commented Apr 19, 2025

I’ve applied the improvements focusing on readability and maintainability.

Here’s how the code looks now — I’m retrieving the required services (NodeScopeResolver and FileAnalyser) using $container->getByType(), as suggested. I also moved the rule and collector registry creation into clearly named variables for better clarity.

Thanks a lot for the great tips — they helped clean this up nicely!

@ondrejmirtes
@owenvoke

Comment thread src/PHPStanAnalyser.php Outdated
@pelmered
Copy link
Copy Markdown

Nice cleanup!

@nunomaduro nunomaduro merged commit d9d0685 into pestphp:3.x Apr 25, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants